package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyzy.domain.ShoppingCar;
import com.cyzy.domain.TCollect;
import com.cyzy.dto.ResponseData;
import com.cyzy.service.TCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyzy.mapper.TCollectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import javax.xml.namespace.QName;
import java.util.UUID;

/**
 * @author 10204
 * @description 针对表【t_collect】商品收藏的数据库操作Service实现
 * @createDate 2024-11-18 11:13:59
 */
@Service
public class TCollectServiceImpl extends ServiceImpl<TCollectMapper, TCollect> implements TCollectService {
    @Autowired
    private TCollectMapper tCollectMapper;

    @Override
    public ResponseData addCollect(TCollect tCollect) {
        QueryWrapper<TCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", tCollect.getUserId())
                .eq("goods_id", tCollect.getGoodsId());

        TCollect existingCollect = tCollectMapper.selectOne(queryWrapper);

        if (existingCollect == null) {
            try {
                int insertResult = tCollectMapper.insert(tCollect);
                if (insertResult > 0) {
                    return new ResponseData(200, "添加收藏成功", tCollect, 0); // 返回收藏信息
                } else {
                    return ResponseData.no2("添加收藏失败");
                }
            } catch (DuplicateKeyException e) {
                return ResponseData.no2("已经在收藏里面了");
            }
        } else {
            return new ResponseData(200, "已经在收藏里面了", existingCollect, 0); // 返回已存在的收藏信息
        }
    }
}




